package cz.cvut.sefcijak.zatacka;

import android.app.Activity;
import android.app.DialogFragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
//nastaveni
//http://viralpatel.net/blogs/android-preferences-activity-example/
import android.content.SharedPreferences;
import android.preference.PreferenceActivity;
// TODO:    MENU
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import android.widget.TextView;
import android.widget.Text;

public class StartActivity extends Activity {

    private static final int RESULT_SETTINGS = 1;

/* TODO:    MENU
* http://developer.android.com/guide/topics/ui/menus.html
* http://www.mysamplecode.com/2011/07/android-options-menu-submenu-group.html
*/

    //public  String vysledky = new String();
    private static final int DIALOG_EXIT = 1;
    private Button mStartButton;
    private Button mNetButton;
    //private Button mScoreButton;
    private static Context context;
    private Intent intent;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        StartActivity.context = getApplicationContext();
        //po startu se automaticky nezobrazi kalvesnice
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        //natstaveni
       // showUserSettings();

        //TODO: Dodělat tisk vysledků
        /*EditText editText = (EditText)findViewById(R.id.textPole);
        editText.setText(Nacti().toString(), TextView.BufferType.EDITABLE);*/

        Button tlStartJenoHra = (Button) findViewById(R.id.tlJednohra);
        Button tlStartNetHra = (Button) findViewById(R.id.tlNet);
        Button tlNastaveni = (Button) findViewById(R.id.tlNastaveni);

        tlStartJenoHra.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                intent  = new Intent(context, GameActivity.class);
                startActivity(intent);
            }
        });
        tlStartNetHra.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                intent  = new Intent(context, NetGameActivity.class);
                startActivity(intent);
            }
        });
        tlNastaveni.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                intent  = new Intent(context, UserSettingActivity.class);
                startActivity(intent);

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

            case R.id.menu_settings:
                Intent i = new Intent(this, UserSettingActivity.class);
                startActivityForResult(i, RESULT_SETTINGS);
                break;
        }
        return true;
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case RESULT_SETTINGS:
                //showUserSettings();
                break;

        }

    }
       /*
    private void showUserSettings() {
        SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
        StringBuilder builder = new StringBuilder();

        builder.append("\n Username: "
                + sharedPrefs.getString("prefUsername", "NULL"));

        builder.append("\n Send report:"
                + sharedPrefs.getBoolean("prefSendReport", false));

        builder.append("\n Sync Frequency: "
                + sharedPrefs.getString("prefSyncFrequency", "NULL"));

        TextView settingsTextView = (TextView) findViewById(R.id.textPole);

        settingsTextView.setText(builder.toString());
    }                   */

    @Override
    protected void onDestroy() {
        super.onDestroy();    //To change body of overridden methods use File | Settings | File Templates.
        finish();
    }

    @Override
    protected void onResume() {
        super.onResume();
        KresliView.bitmapa = KresliView.bitmapaCista;
    }

    /*
    @Override
    protected void onPause() {
        super.onPause();
        //finish();
        Intent i = new Intent(this, GameActivity.class);
        startActivity(i);
        finish();
    } * /

    @Override
    protected void onRestart() {
        super.onRestart();
        //finish();
        Intent i = new Intent(this, GameActivity.class);
        startActivity(i);
        finish();

    }   */

     /*
    public void Pripis(String text) {
        String eol = System.getProperty("line.separator");
        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new OutputStreamWriter(openFileOutput("myfile", MODE_WORLD_WRITEABLE)));
            writer.write(vysledky + eol);
            writer.write("This is a test2." + eol);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public StringBuffer Nacti() {
        String eol = System.getProperty("line.separator");
        BufferedReader input = null;
        try {
            input = new BufferedReader(new InputStreamReader(openFileInput("myfile")));
            String line;
            StringBuffer buffer = new StringBuffer();
            while ((line = input.readLine()) != null) {
                buffer.append(line + eol);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }         */
}

